package com.rainbow.controller

import com.rainbow.entity.User
import com.rainbow.service.UserService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody

@Controller
class HomeController {

    @Autowired
    lateinit var userService: UserService

    @GetMapping("/", "/index", "/home")
    fun root(): String {
        return "index"
    }


    @GetMapping("/login")
    fun login(): String {
        return "login"
    }

    @GetMapping("/register")
    fun register(): String {
        return "register"
    }

    @PostMapping("/register")
    fun doRegister(user: User): String {
        val response: String;
        if (userService.insert(user)) {
            response = "redirect:register?success"
        } else {
            response = "redirect:register?error"
        }
        return response
    }

}